package com.lnsoft.dqfs.server.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lnsoft.dqfs.server.dao.Yc;

import io.netty.buffer.ByteBuf;

public class Line2Yc {
	private static final Logger log = LoggerFactory.getLogger(Line2Yc.class);

	//	解析未加密的字节流，返回Yc实体类(Yc为腐蚀量信息的实体类)
    public static List<Yc> bytes2YcList(int clientName,ByteBuf byteBuf){
    	List<Yc> list = new ArrayList<Yc>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        try {
        	String line = null;
        	while(true) {
        		int index = byteBuf.bytesBefore((byte) 10);
                //  如果不存在换行符，则退出循环读取行数据
                if (index <= 0) {
                	//	找不到换行符，则退出循环
                    break;
                }
                byte[] lineArray = new byte[index];
                byteBuf.readBytes(lineArray);
                line = new String(lineArray);
                //  读掉行尾的换行符
                byteBuf.readByte();
                
                //  检查读取的一行数据
                if (line == null || !line.startsWith("16")) {
                    log.debug("客户端{}:{}，文件数据行异常:文件行为null或者不是‘16’开头", clientName);
                    continue;
                }


                line = line.replace("\n", "").trim();

                if (line.lastIndexOf("16,2") > 1) {
                    line = line.substring(line.lastIndexOf("16,2"));
                }

                String[] datas = line.split(",");

                //  检查数据完整性，一组数据应该是34个字段
                if (datas.length != 34) {
                    log.debug("客户端{}，文件数据行异常:该行数据不足34个字段", clientName);
                    continue;
                }

                Yc yc = new Yc();
            	yc.setCjsj(sdf.format(sdf2.parse(datas[1])));
            	yc.setTyndy(MessageUtil.upNanVal(datas[3].trim()));
            	yc.setDcdy(MessageUtil.upNanVal(datas[5].trim()));
            	yc.setJxwd(MessageUtil.upNanVal(datas[7].trim()));
            	yc.setJxsd(MessageUtil.upNanVal(datas[9].trim()));
            	yc.setGprs(MessageUtil.upNanVal(datas[11].trim()));
            	yc.setHjwd(MessageUtil.upNanVal(datas[13].trim()));
            	yc.setHjsd(MessageUtil.upNanVal(datas[15].trim()));
            	yc.setSond(MessageUtil.upNanVal(datas[17].trim()));
            	yc.setYf(MessageUtil.upNanVal(datas[19].trim()));
            	yc.setBy1(MessageUtil.upNanVal(datas[21].trim()));
            	yc.setBy2(MessageUtil.upNanVal(datas[23].trim()));
            	yc.setBy3(MessageUtil.upNanVal(datas[25].trim()));
            	yc.setZndl(MessageUtil.upNanVal(datas[27].trim()));
            	yc.setCudl(MessageUtil.upNanVal(datas[29].trim()));
            	yc.setAldl(MessageUtil.upNanVal(datas[31].trim()));
            	yc.setFedl(MessageUtil.upNanVal(datas[33].trim()));
                
                list.add(yc);
        	}
        } catch (Exception e) {
			// TODO: handle exception
        	e.printStackTrace();
		}
    	
    	return list;
    }
}
